package cc.linker.steplink.crm.merchant.controller;


import cc.linker.steplink.crm.merchant.dao.PersonnelDao;
import cc.linker.steplink.crm.merchant.pojo.PersonnelReadDTO;
import cc.linker.steplink.crm.merchant.service.PersonnelService;
import cc.linker.steplink.crm.merchanth5.commons.UserInfoUtil;
import cc.linker.steplink.crm.response.GlobalException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.sf.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 人才星计划
 */
@RestController
@RequestMapping("personnel")
public class PersonnelController {
    @Autowired
    private PersonnelDao personnelDao;

    private final UserInfoUtil userInfoUtil;
    private final PersonnelService personnelService;

    public PersonnelController(UserInfoUtil userInfoUtil, PersonnelService personnelService) {
        this.userInfoUtil = userInfoUtil;
        this.personnelService = personnelService;
    }

    /**
     * 列表
     *
     * @param name
     * @param area
     * @param pageIndex
     * @param pageSize
     * @param email
     * @param mechanism
     * @param domain
     * @param request
     * @return
     */
    @GetMapping("/getList")
    public PageInfo<Map<String, Object>> getList(
            @RequestParam(value = "name", defaultValue = "", required = false) String name,
            @RequestParam(value = "area", defaultValue = "", required = false) String area,
            @RequestParam(value = "pageIndex", defaultValue = "1", required = false) Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "20", required = false) Integer pageSize,
            @RequestParam(value = "email", defaultValue = "", required = false) String email,
            @RequestParam(value = "mechanism", defaultValue = "", required = false) String mechanism,
            @RequestParam(value = "domain", defaultValue = "", required = false) String domain,
            @RequestParam(value = "type", defaultValue = "", required = false) String type,
            HttpServletRequest request) {
//        PcUserInfo userInfo = this.userInfoUtil.getPcUserInfo(request);
        String orgId = "10147";
        Map<String, String> map = new HashMap<>();
        map.put("orgId", orgId);
        if (!StringUtils.isEmpty(name)) {
            map.put("name", name);
        }
        if (!StringUtils.isEmpty(area)) {
            map.put("area", area);
        }
        if (!StringUtils.isEmpty(email)) {
            map.put("email", email);
        }
        if (!StringUtils.isEmpty(mechanism)) {
            map.put("mechanism", mechanism);
        }
        if (!StringUtils.isEmpty(domain)) {
            map.put("domain", domain);
        }
        if(!StringUtils.isEmpty(type)){
            map.put("type",type);
        }
        PageHelper.startPage(pageIndex, pageSize);
        return new PageInfo<>(personnelService.getList(map));
    }


    /**
     * 获取人才详情
     *
     * @param authorId
     * @return
     */
    @GetMapping("/getDetail")
    public Map<String, Object> getDetail(@RequestParam(value = "authorId",required = true) String authorId) {
        if (StringUtils.isEmpty(authorId)) {
            throw GlobalException.build("参数错误");
        }
        return personnelService.getDetail(authorId);
    }

    /**
     * 人才数据添加到customer主表
     */
    @GetMapping("/addCustomer")
    public void addCustomer(HttpServletRequest request) {
        String orgId = "10147";
        personnelService.addCustomer(orgId);
    }


    /**
     * 删除
     *
     * @param map
     */
    @PostMapping("/del")
    public void del(@RequestBody Map<String, Object> map) {
        if (StringUtils.isEmpty(map.get("authorId"))) {
            throw GlobalException.build("参数错误");
        }
        personnelService.del(Long.parseLong(map.get("authorId").toString()));
    }

    @PostMapping("/saveRemark")
    public void saveRemark(@RequestBody Map<String, Object> map) {
        if (StringUtils.isEmpty(map.get("authorId"))) {
            throw GlobalException.build("参数错误");
        }
        String remark = "";
        if (!StringUtils.isEmpty(map.get("remark"))) {
            remark = map.get("remark").toString();
        }
        personnelService.saveRemark(map.get("authorId").toString(), remark);
    }

    /**
     * 获取领域列表
     *
     * @param request
     * @return
     */
    @GetMapping("/getDomainList")
    public HashSet<String> getDomainList(HttpServletRequest request) {
        String orgId = "10147";
        return personnelService.getDomainList(orgId);
    }


    //推送报告
    @GetMapping("/pushReport")
    public List<Map<String, String>> pushReport() {
        return personnelDao.pushReport();
    }

    /**
     * 按照用户类型进行分组统计
     * @return
     */
    @GetMapping("/getTypeCount")
    public Object selectPersonelCountByType(){
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("a","a");
        List<Map<String, String>> list = personnelService.selectPersonelCountByType(paramMap);
        for (int i = 0; i < CollectionUtils.size(list); i++) {
            list.get(i).put("typeName","测试分组"+list.get(i).get("type"));
        }
        return list;
    }


}
